using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;
using FiltronASP.Helpers.Translations;
using FiltronASP.Helpers.CatalogueHelpers;
using System.IO;
using System.Net;
using FiltronASP.Logger;
using FiltronASP.Helpers.ReplacementsHelper;
using System.Text;
using FiltronASP.Consts;

namespace FiltronASP.Helpers.AjaxModel
{
    public static class SingleFilterImagesHelper
    {
        private static string getDefaultFilterImg(string languageName)
        {
            string defaultFilterImages = VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter_english.png");
            if (String.IsNullOrEmpty(languageName))
            {
                return defaultFilterImages;
            }   //if
            if (languageName == "polish")
            {
                return VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter.png");
            }   //if
            return defaultFilterImages;
        }

        private static string getDefaultLargeFilterImg(string languageName)
        {
            string defaultFilterImagesLarge = VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter_english.png");
            if (String.IsNullOrEmpty(languageName))
            {
                return defaultFilterImagesLarge;
            }   //if
            if (languageName == "polish")
            {
                return VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter.png");
            }   //if
            return defaultFilterImagesLarge;
        }

        private static string getExtraPhoto(string filterNumber, string languageName, string extraPhotoName)
        {
            string filterImg = SingleFilterImagesHelper.getDefaultFilterImg(languageName);
            string filterLargeImg = SingleFilterImagesHelper.getDefaultLargeFilterImg(languageName);
            StringBuilder sb = new StringBuilder();
            if ((!String.IsNullOrEmpty(filterNumber)) && (!String.IsNullOrEmpty(extraPhotoName)))
            {
                if (File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/" + extraPhotoName)))
                {
                    filterImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/" + extraPhotoName);
                    filterLargeImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/1500/" + extraPhotoName);
                }
                if (File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/1500/" + extraPhotoName)))
                {
                    if (!File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/" + extraPhotoName)))
                    {
                        filterImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/1500/" + extraPhotoName);
                    }   //if
                    filterLargeImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/1500/" + extraPhotoName);
                }
            }   //if
            else
            {
                filterNumber = "---";
            }   //else
            sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", filterLargeImg, filterNumber, filterImg));
            sb.Append("</a>");
            return sb.ToString();
        }

        private static string getAxisPhoto(string filterNumber, string languageName, string axisPhotoName)
        {
            string filterImg = SingleFilterImagesHelper.getDefaultFilterImg(languageName);
            string filterLargeImg = SingleFilterImagesHelper.getDefaultLargeFilterImg(languageName);
            StringBuilder sb = new StringBuilder();
            if ((!String.IsNullOrEmpty(filterNumber)) && (!String.IsNullOrEmpty(axisPhotoName)))
            {
                if (File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/0750/images/" + axisPhotoName)))
                {
                    filterImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/0750/images/" + axisPhotoName);
                    filterLargeImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/1500/images/" + axisPhotoName);
                }
                if (File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/1500/images/" + axisPhotoName)))
                {
                    if (!File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/750/images/" + axisPhotoName)))
                    {
                        filterImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/1500/images/" + axisPhotoName);
                    }   //if
                    filterLargeImg = VirtualPathUtility.ToAbsolute("~/Content/Assets/Filters/" + filterNumber + "/1500/images/" + axisPhotoName);
                }
            }   //if
            else
            {
                filterNumber = "---";
            }   //else
            sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", filterLargeImg, filterNumber, filterImg));
            sb.Append("</a>");
            return sb.ToString();
        }

        private static bool checkExtraPhotoExists(string filterNumber, string languageName, string extraPhotoName)
        {
            if ((!String.IsNullOrEmpty(filterNumber)) && (!String.IsNullOrEmpty(extraPhotoName)))
            {
                return ((File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/" + extraPhotoName)))
                    || (File.Exists(HttpContext.Current.Server.MapPath("~/Content/Assets/Filters/" + filterNumber + "/1500/" + extraPhotoName))));
            }   //else
            return false;
        }

        private static bool checkOldPhotoExists(string filterNumber, string languageName)
        {
            if (!String.IsNullOrEmpty(filterNumber))
            {
                return ((File.Exists(HttpContext.Current.Server.MapPath("~/Content/img/filters/Filtron/" + filterNumber + ".jpg")))
                    || (File.Exists(HttpContext.Current.Server.MapPath("~/Content/img/filters/" + filterNumber + ".png"))));
            }   //else
            return false;
        }

        private static string getOldPhoto(string filterNumber, string languageName)
        {
            StringBuilder sb = new StringBuilder();
            if ((!String.IsNullOrEmpty(filterNumber)) || (!String.IsNullOrEmpty(filterNumber)))
            {
                string imageFilter = HttpContext.Current.Server.MapPath("~/Content/img/filters/Filtron/" + filterNumber + ".jpg");
                if (File.Exists(imageFilter))
                {
                    sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", VirtualPathUtility.ToAbsolute("~/Content/img/filters/Filtron/large/" + filterNumber + ".jpg"), filterNumber, VirtualPathUtility.ToAbsolute("~/Content/img/filters/Filtron/" + filterNumber + ".jpg")));
                    sb.Append("</a>");
                    return sb.ToString();
                }
                imageFilter = HttpContext.Current.Server.MapPath("~/Content/img/filters/" + filterNumber + ".png");
                if (File.Exists(imageFilter))
                {
                    sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", VirtualPathUtility.ToAbsolute("~/Content/img/filters/" + filterNumber + ".png"), filterNumber, VirtualPathUtility.ToAbsolute("~/Content/img/filters/thumbnails/" + filterNumber + ".png")));
                    sb.Append("</a>");
                    return sb.ToString();
                }
                if (languageName == "polish")
                {
                    sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter.png"), "---", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter.png")));
                    sb.Append("</a>");
                    return sb.ToString();
                }   //if
            }   //if
            sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter_english.png"), "---", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter_english.png")));
            sb.Append("</a>");
            return sb.ToString();
        }

        private static string getFiltronRepresentativePhoto(int? filterMediaId, string languageName)
        {
            StringBuilder sb = new StringBuilder();
            if (filterMediaId.HasValue)
            {
                IFiltronFilterMediaDAO filtronFilterMediaDAO = new FiltronFilterMediaDAO();
                FiltronFilterMedia filterMedia = filtronFilterMediaDAO.FindByPK(filterMediaId.Value);
                string orgFilterMedia = String.Empty;
                if (filterMedia != null)
                {
                    orgFilterMedia = filterMedia.media_name;
                    string repImageName = FiltronHelper.GetFiltronFilterImage(orgFilterMedia);
                    string imageFilter = HttpContext.Current.Server.MapPath(String.Format("~/Content/img/filters/Filtron/{0}.jpg", repImageName));
                    if (File.Exists(imageFilter))
                    {
                        sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", VirtualPathUtility.ToAbsolute(String.Format("~/Content/img/filters/Filtron/{0}.jpg", repImageName)), repImageName, VirtualPathUtility.ToAbsolute(String.Format("~/Content/img/filters/Filtron/{0}.jpg", repImageName))));
                        sb.Append("</a>");
                        return sb.ToString();
                    }
                }
            }   //if
            if (languageName == "polish")
            {
                sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter.png"), "---", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter.png")));
                sb.Append("</a>");
                return sb.ToString();
            }   //if
            sb.Append(String.Format("<a style='background-image: url(\"{2}\")' href='{0}' data-lightbox='image-1' data-title='{1}'>", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter_english.png"), "---", VirtualPathUtility.ToAbsolute("~/Content/img/temp/example_filter_english.png")));
            sb.Append("</a>");
            return sb.ToString();
        }

        public static string generateFirstFilterPhoto(string filterNumber, string languageName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-md-3 col-sm-4 col-xs-6 mar-b-20'>");
            sb.Append("<div class='download-files small-image-filter-details'>");
            sb.Append(SingleFilterImagesHelper.getOldPhoto(filterNumber, languageName));            
            sb.Append("</div>");
            sb.Append("</div>");
            return sb.ToString();
        }

        public static string generateFirstFilterPhoto(string filterNumber, int? filterMediaId, string languageName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-md-3 col-sm-4 col-xs-6 mar-b-20'>");
            sb.Append("<div class='download-files small-image-filter-details'>");
            if (SingleFilterImagesHelper.checkOldPhotoExists(filterNumber, languageName))
            {
                sb.Append(SingleFilterImagesHelper.getOldPhoto(filterNumber, languageName));
            }   //if
            else
            {
                sb.Append(SingleFilterImagesHelper.getFiltronRepresentativePhoto(filterMediaId, languageName));
            }   //else
            sb.Append("</div>");
            sb.Append("</div>");
            return sb.ToString();
        }

        public static string generateSecondFilterPhoto(string filterNumber, string languageName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-md-3 col-sm-4 col-xs-6 mar-b-20'>");
            sb.Append("<div class='download-files small-image-filter-details'>");
            if (SingleFilterImagesHelper.checkExtraPhotoExists(filterNumber, languageName, GlobalConsts.EXTRA_PHOTO_1_NAME))
            {
                sb.Append(SingleFilterImagesHelper.getExtraPhoto(filterNumber, languageName, GlobalConsts.EXTRA_PHOTO_1_NAME));
            }
            else
            {
                sb.Append(SingleFilterImagesHelper.getAxisPhoto(filterNumber, languageName, GlobalConsts.AXIS_PHOTO_1_NAME));
            }   //else
            sb.Append("</div>");
            sb.Append("</div>");
            return sb.ToString();
        }

        public static string generateThirdFilterPhoto(string filterNumber, string languageName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-md-3 col-sm-4 col-xs-6 mar-b-20'>");
            sb.Append("<div class='download-files small-image-filter-details'>");
            if (SingleFilterImagesHelper.checkExtraPhotoExists(filterNumber, languageName, GlobalConsts.EXTRA_PHOTO_2_NAME))
            {
                sb.Append(SingleFilterImagesHelper.getExtraPhoto(filterNumber, languageName, GlobalConsts.EXTRA_PHOTO_2_NAME));
            }
            else
            {
                sb.Append(SingleFilterImagesHelper.getAxisPhoto(filterNumber, languageName, GlobalConsts.AXIS_PHOTO_2_NAME));
            }   //else
            sb.Append("</div>");
            sb.Append("</div>");
            return sb.ToString();
        }

        public static string generateFourthFilterPhoto(string filterNumber, string languageName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-md-3 col-sm-4 col-xs-6 mar-b-20'>");
            sb.Append("<div class='download-files small-image-filter-details'>");
            sb.Append(SingleFilterImagesHelper.getAxisPhoto(filterNumber, languageName, GlobalConsts.AXIS_PHOTO_3_NAME));
            sb.Append("</div>");
            sb.Append("</div>");
            return sb.ToString();
        }

    }
}
